11/14/2017

Using ggplot2 and friends

ggplot2 graphics

data(diamonds)
ggplot(diamonds, aes(x = carat, y = price, color = color))+
  geom_point()

ggplot2 graphics

data(diamonds)
ggplot(diamonds, aes(x = carat, y = price, color = color))+
  geom_point() + 
  scale_x_continuous(name = "Carat", breaks = 1:5, minor_breaks = NULL)+
  scale_y_log10(name = "Price ($)")+
  labs(color = "Color")

ggplot2 graphics

ggplot(diamonds, aes(x = carat, y = price, color=clarity, size = depth))+
  geom_point()

ggplot2 graphics

  • Hexagonal binning is useful if you have many many overlapping points.
# Requires the "hexbin" package to be installed
ggplot(diamonds, aes(x = carat, y = price))+
  geom_hex()

ggplot2 graphics

ggplot(diamonds, aes(x = cut, y = price)) + geom_boxplot() +
  scale_y_log10()+
  labs(x = 'Cut',y = 'Price ($)') + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggplot2 graphics

ggplot(diamonds, aes(x = cut, y = price)) + geom_boxplot() +
  scale_y_log10()+
  labs(x = 'Cut',y = 'Price ($)') + 
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  coord_flip()

# Adding ggthemes

ggplot2 graphics + ggthemes

library(ggthemes)
ggplot(diamonds, aes(x=carat, y = price, color = color))+
  geom_point()+
  theme_economist()

ggplot2 + ggthemes

data(diamonds)
ggplot(diamonds, aes(x = carat, y = price, color = color))+
  geom_point()+
  theme_fivethirtyeight()

ggplot2 + ggthemes

data(diamonds)
ggplot(diamonds, aes(x = carat, y = price, color = color))+
  geom_point()+
  theme_excel()

ggplot2 + ggthemes

data(diamonds)
ggplot(diamonds, aes(x = carat, y = price, color = color))+
  geom_point()+
  theme_wsj()

ggplot2 + ggthemes

data(diamonds)
ggplot(diamonds, aes(x = carat, y = price, color = color))+
  geom_point()+
  theme_stata()

ggplot2 + ggthemes

data(diamonds)
ggplot(diamonds, aes(x = carat, y = price, color = color))+
  geom_point()+
  theme_few()

Using cowplot

Cowplot

suppressPackageStartupMessages(library(cowplot))
plt <- ggplot(diamonds, aes(x = carat, y = price, color=clarity))+
  geom_point()
plt

Cowplot

plt + background_grid(major = 'xy', minor = 'none')

Cowplot

plt1 <- ggplot(diamonds, aes(x = carat, y = price)) + geom_point()
plt2 <- ggplot(diamonds, aes(x = clarity, y = price)) + geom_boxplot()
cowplot::plot_grid(plt1, plt2, labels = c('A','B'))

Cowplot

plt3 <- ggplot(diamonds, aes(x = color, y = price)) + geom_boxplot()
cowplot::plot_grid(plt1, plt2, plt3, labels = c('A','B','C'), nrow=2)

Other extensions

ggExtra

library(ggExtra)
p <- ggplot(diamonds, aes(x = carat, y = price)) + geom_point()
ggMarginal(p, type = 'histogram')

Survival plots

Survival plots

library(survival)
library(survminer)
fit <- survfit(Surv(time, status) ~ sex, data = lung)
ggsurvplot(fit, data = lung)

Survival plots

ggsurvplot(
  fit, 
  data = lung, 
  size = 1,                 # change line size
  palette = 
    c("#E7B800", "#2E9FDF"),# custom color palettes
  conf.int = TRUE,          # Add confidence interval
  pval = TRUE,              # Add p-value
  risk.table = TRUE,        # Add risk table
  risk.table.col = "strata",# Risk table color by groups
  legend.labs = 
    c("Male", "Female"),    # Change legend labels
  risk.table.height = 0.25, # Useful to change when you have multiple groups
  ggtheme = theme_bw()      # Change ggplot2 theme
)

Survival plots